import store from "@/store";
import { createRouter, createWebHistory } from "vue-router";
import { Toast } from 'vant';
const Home = () => import("@/views/home/Home");
const Category = () => import("@/views/category/Category");
const Detail = () => import("@/views/detail/Detail");
const Profile = () => import("@/views/profile/Profile");
const ShopCart = () => import("@/views/shopcart/ShopCart");
const Register = () => import('@/views/profile/Register');
const Login = () => import('@/views/profile/Login');
const Address = () => import('@/views/profile/Address');
const AddressEdit = () => import('@/views/profile/AddressEdit');
const Order = () => import('@/views/order/Order');
const CreateOrder = () => import('@/views/order/CreateOrder');
const OrderDetail = () => import('@/views/order/OrderDetail');

const routes = [
  {
    path: "/",
    name: "DefaultHome",
    component: Home,
    meta: {
      title: '图书兄弟-商品首页'
    }
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    meta: {
      title: '图书兄弟-商品首页'
    }
  },
  {
    path: "/category",
    name: "Category",
    component: Category,
    meta: {
      title: '图书兄弟-商品分类'
    }
  },
  {
    path: "/detail",
    name: "Detail",
    component: Detail,
    meta: {
      title: '图书兄弟-商品详情'
    }
  },
  {
    path: "/shopcart",
    name: "Shopcart",
    component: ShopCart,
    meta: {
      title: '图书兄弟-购物车'
    }
  },
  {
    path: "/profile",
    name: "Profile",
    component: Profile,
    meta: {
      title: '图书兄弟-个人中心',
      isAuthRequired: true, //表示是否需要确认授权
    }
  },
  {
    path: "/address",
    name: "Address",
    component: Address,
    meta: {
      title: '图书兄弟-地址',
      isAuthRequired: true, //表示是否需要确认授权
    }
  },
  {
    path: "/addressedit",
    name: "AddressEdit",
    component: AddressEdit,
    meta: {
      title: '图书兄弟-地址编辑',
      isAuthRequired: true, //表示是否需要确认授权
    }
  },
  {
    path: '/order',
    name: 'Order',
    component: Order,
    meta: {
      title: '图书兄弟-生成订单',
      isAuthRequired: true
    }
  },
  {
    path: '/create-order',
    name: 'CreateOrder',
    component: CreateOrder,
    meta: {
      title: '图书兄弟-订单预览',
      isAuthRequired: true
    }
  },
  {
    path: '/order-detail',
    name: 'OrderDetail',
    component: OrderDetail,
    meta: {
      title: '图书兄弟-订单详情',
      isAuthRequired: true
    }
  },
  {
    path: "/register",
    name: "register",
    component: Register,
    meta: {
      title: '图书兄弟-用户注册'
    }
  },
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: {
      title: '图书兄弟-用户登录'
    }
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

// 这个方法是在路由切换之前执行
router.beforeEach((to, from, next) => {
  // 通过在路由元数据中定义的 isAuthRequired来判断当前路由是否需要授权登录后才能访问
  if (to.meta.isAuthRequired && store.state.user.isLogin === false) {
    Toast.fail('当前未登录，即将跳转到登录页面');
    return next('/login')
  } else {
    next();
  }


  next();
  document.title = to.meta.title;
})
export default router;
